﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections.ObjectModel;
using DbSharp.Framework.CaseFile.Parser;


namespace DbSharp.GuiRunner.ViewItems
{
    [Obfuscation]
    enum CaseFileObjectType
    {
        DbConnection,
        Statement,
        PerfTest,
        UnitTest,
        EntireFile,
        GlabalSetting,
        ProcDeclarations,
        SqlDeclarations
    }


    class CaseFileObject
    { 
        public string Name { get; set; }
        public CaseFileObjectType Type { get; set; }
          

        public override string ToString()
        {
            string objectFmt = "{0}-{1}";
            string setFmt = "[{0}]-{1}";
            string result = string.Empty; ;
            switch (Type)
            {
                case CaseFileObjectType.DbConnection:
                    result = string.Format(objectFmt, "DB", Name);
                    break;
                case CaseFileObjectType.PerfTest:
                    result = string.Format(objectFmt, "Perf", Name);
                    break;
                case CaseFileObjectType.UnitTest:
                    result = string.Format(objectFmt, "Unit", Name);
                    break;
                case CaseFileObjectType.Statement:
                    result = string.Format(objectFmt, "DbStmt", Name);
                    break;
                case CaseFileObjectType.GlabalSetting:
                    result = string.Format(setFmt, "GlobalSetting", Name);
                    break;
                case CaseFileObjectType.ProcDeclarations:
                    result = string.Format(setFmt, "Procs", Name);
                    break;
                case CaseFileObjectType.SqlDeclarations:
                    result = string.Format(setFmt, "Sqls", Name);
                    break;
                case CaseFileObjectType.EntireFile:
                    result = string.Format(setFmt, "EntireFile", Name);
                    break;
                default:
                    break;
            }
            return result;
        }
    }



    class CaseFileObjectLoader
    {
        #region Private members

        List<CaseFileObject> m_ListCaseFileObject = new List<CaseFileObject>();
        CaseFileParser m_TestcaseParser;

        #endregion

        public CaseFileObjectLoader(CaseFileParser testcaseParser)
        {
            this.m_TestcaseParser = testcaseParser;
            LoadCaseFileObjects();
        }


        #region private methods

        private void LoadCaseFileObjects()
        {
            m_ListCaseFileObject.Clear();

            #region Load ordernary objects

            //load unitTest cases
            foreach (var item in m_TestcaseParser.ListUnitTestCase)
            {
                m_ListCaseFileObject.Add(new CaseFileObject
                {
                    Name = item.TestCaseName,
                    Type = CaseFileObjectType.UnitTest
                });
            }

            //load PerfTest cases
            foreach (var item in m_TestcaseParser.ListPerfTestCase)
            {
                m_ListCaseFileObject.Add(new CaseFileObject
                {
                    Name = item.TestCaseName,
                    Type = CaseFileObjectType.PerfTest
                });
            }

            //load DbConnection
            foreach (var item in m_TestcaseParser.ListDbConnCofig)
            {
                m_ListCaseFileObject.Add(new CaseFileObject
                {
                    Name = item.ConnectionName,
                    Type = CaseFileObjectType.DbConnection
                });
            }

            //load statement items
            foreach (var item in m_TestcaseParser.ListStatement)
            {
                m_ListCaseFileObject.Add(new CaseFileObject
                {
                    Name = item.Alias,
                    Type = CaseFileObjectType.Statement
                });
            }

            #endregion


            #region Load dummy objects

            //load dummy SqlDeclarations
            m_ListCaseFileObject.Insert(0, new CaseFileObject
            {
                Name = "[SqlDeclarations]",
                Type = CaseFileObjectType.SqlDeclarations
            });

            //load dummy procDeclarations
            m_ListCaseFileObject.Insert(0, new CaseFileObject
            {
                Name = "[ProcDeclarations]",
                Type = CaseFileObjectType.ProcDeclarations
            });



            //load dummy globalSetting
            m_ListCaseFileObject.Insert(0, new CaseFileObject
            {
                Name = "[GlabalSetting]",
                Type = CaseFileObjectType.GlabalSetting
            });

            //load entirefile root element
            m_ListCaseFileObject.Insert(0, new CaseFileObject
            {
                Name = "[EntireFile]",
                Type = CaseFileObjectType.EntireFile
            });

            #endregion

            //sort items
            m_ListCaseFileObject.Sort((x, y) =>
                    string.Compare(x.ToString(), y.ToString(), StringComparison.OrdinalIgnoreCase)
                 );
        }



        #endregion

        #region Public property/method

        public ReadOnlyCollection<CaseFileObject> ListCaseFileObject
        {
            get
            {
                return new ReadOnlyCollection<CaseFileObject>(m_ListCaseFileObject);
            }
        }

        #endregion
    }
}
